package kg.aibek.projecteuler.p10;

import java.util.ArrayList;

public class Problem50 {
	public void solve() {
		ArrayList<Integer> list = new ArrayList<Integer>();
		int i = 2;
		while (i < 1000000) {
			if (isPrime(i))
				list.add(i);

			i++;
		}

		long[] a = new long[list.size() + 1];
		a[0] = 0;
		for (i = 1; i <= list.size(); i++) {
			a[i] = list.get(i - 1) + a[i - 1];
		}

		long max = 0;
		int seq = 0;
		for (i = 0; i < a.length; i++) {
			if (i % 100 == 0) {
				System.out.print(".");
			}
			for (int j = i + 1; j < a.length; j++) {
				if (j - i < seq) {
					continue;
				}
				if (a[j] - a[i] < 1000000 && isPrime(a[j] - a[i])) {
					if (j - i > seq) {
						max = a[j] - a[i];
						seq = j - i;
					}
				}
			}
		}

		System.out.println("\n" + max);
	}

	private boolean isPrime(long number) {
		for (int i = 2; i * i <= number; i++) {
			if (number % i == 0)
				return false;
		}
		return true;
	}

	public static void main(String[] args) {
		Problem50 p = new Problem50();
		p.solve();
	}
}
